package com.chinameyer.equipment_ebook.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinameyer.equipment_ebook.bean.Event;
import com.chinameyer.equipment_ebook.bean.ManualType;
import com.chinameyer.equipment_ebook.dto.UploadMsgDTO;
import com.chinameyer.equipment_ebook.mapper.EventMapper;
import com.chinameyer.equipment_ebook.mapper.ManualTypeMapper;
import com.chinameyer.equipment_ebook.vo.DocumentsVO;
import com.chinameyer.equipment_ebook.vo.DocumentsVVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author meyer@HongYe
 */
@Service
public class EventService {

    @Autowired
    private EventMapper eventMapper;
    @Autowired
    private ManualTypeMapper manualTypeMapper;

    public String checkManualIsExist(String equipmentNo, List<UploadMsgDTO> UploadMsgDTOList) {
        QueryWrapper<Event> wrapper = new QueryWrapper<>();
        for (UploadMsgDTO uploadMsgDTO:UploadMsgDTOList) {
            wrapper.lambda().eq(Event::getEquipmentNo,equipmentNo).eq(Event::getTypeId,uploadMsgDTO.getManualTypeId());
            boolean isExist = this.eventMapper.selectCount(wrapper) > 0;
            if(isExist){
                String type = this.manualTypeMapper.selectById(uploadMsgDTO.getManualTypeId()).getType();
                return "设备：" + equipmentNo + "已经存在" + type + "，请勿重复添加！";
            }
        }
        return null;
    }

    public boolean addDocument(String equipmentNo, List<UploadMsgDTO> UploadMsgDTOList, Integer userId) {
        for (UploadMsgDTO uploadMsgDTO:UploadMsgDTOList) {
            Event event = new Event();
            event.setEquipmentNo(equipmentNo).setUrl(uploadMsgDTO.getUrl()).setTypeId(uploadMsgDTO.getManualTypeId()).setUserId(userId);
            boolean result = this.eventMapper.insert(event) > 0;
            if(!result){
                return false;
            }
        }
        return true;
    }

    public boolean updateDocument(Integer id, String url) {
        Event event = this.eventMapper.selectById(id);
        if(StringUtils.isEmpty(event)){
            return false;
        }
        event.setUrl(url);
        return this.eventMapper.updateById(event) > 0;
    }

    public List<DocumentsVVO> getAllDocuments(String equipmentNo, String manualType, Integer userId, Integer count, Integer page) {
        int start = count * page;
        int limit = count;
        List<String> equipmentNoList = this.eventMapper.selectEquipmentNoByUserId(equipmentNo,manualType,userId,start,limit);
        List<DocumentsVVO> documentsVVOList = new ArrayList<>();
        for (String equipmentNoTemp:equipmentNoList) {
            List<DocumentsVO> documentsVOList = this.eventMapper.getAllDocuments(equipmentNoTemp);
            DocumentsVVO documentsVVO = new DocumentsVVO();
            documentsVVO.setEquipmentNo(equipmentNoTemp);
            documentsVVO.setDocuments(documentsVOList);
            documentsVVOList.add(documentsVVO);
        }
        return documentsVVOList;
    }

    public int getTotalDocuments(String equipmentNo, String manualType, Integer userId) {
        List<String> equipmentNoList = this.eventMapper.getTotalDocuments(equipmentNo,manualType,userId);
        return equipmentNoList.size();
    }

    public boolean getAllContext(Integer manualTypeId, Integer userId) {
        QueryWrapper<Event> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Event::getTypeId,manualTypeId).eq(Event::getUserId,userId);
        return this.eventMapper.selectCount(wrapper) > 0;
    }
}
